package com.candoit.webapps.pricing.bean;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

@Component("context")
public class PricingContext {
	
	@PersistenceContext
	private EntityManager entityManager = null;
	
	private EntityManagerFactory entityManagerFactory = null;
	
	private static PricingContext instance= null;
	
	public static PricingContext getInstance(){
		if (instance == null){
			instance = new PricingContext();
		}
		
		return instance;
	}

	public EntityManager getEntityManager() {
		if (entityManager == null){
			entityManager = getEntityManagerFactory().createEntityManager();
		}
		return entityManager;
	}
	
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public EntityManagerFactory getEntityManagerFactory() {
		if (entityManagerFactory == null){
			entityManagerFactory = Persistence.createEntityManagerFactory("acme");
		}
		return entityManagerFactory;
	}
	
	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}
	
}
